package com.study.mcpserverwebflux.service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;

@Service
public class WeatherService {

    private static final String BASE_URL = "https://ali-weather.showapi.com/gps-to-weather";

    private final RestClient restClient;

    public WeatherService() {

        this.restClient = RestClient.builder()
                .baseUrl(BASE_URL)
                .defaultHeader("Accept", "application/geo+json")
                .defaultHeader("Authorization", "APPCODE ".concat("5806b59e3280496ca448e10d3002284e"))
                .build();
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public record WeatherApiResponse(
            @JsonProperty("showapi_res_body") ShowApiResBody showApiResBody
    ) {
        @JsonIgnoreProperties(ignoreUnknown = true)
        public record ShowApiResBody(
                @JsonProperty("cityInfo") CityInfo cityInfo,
                @JsonProperty("now") CurrentWeather now
        ) {
            @JsonIgnoreProperties(ignoreUnknown = true)
            public record CityInfo(
                    @JsonProperty("c3") String cityName
            ) {}

            @JsonIgnoreProperties(ignoreUnknown = true)
            public record CurrentWeather(
                    @JsonProperty("temperature") String temperature,
                    @JsonProperty("weather") String weather,
                    @JsonProperty("temperature_time") String temperatureTime
            ) {}
        }
    }

    /**
     * Get forecast for a specific latitude/longitude
     *
     * @param latitude  Latitude
     * @param longitude Longitude
     * @return The forecast for the given location
     * @throws RestClientException if the request fails
     */
    @Tool(description = "通过经纬度获取天气预测")
    public String getWeatherForecastByLocation(double latitude, double longitude) {

        var forecast = restClient.get()
                .uri(uriBuilder -> uriBuilder
                        .queryParam("lat", latitude)
                        .queryParam("lng", longitude)
                        .build())
                .retrieve()
                .body(WeatherApiResponse.class);

        return String.format("""
                        %s:
                        Temperature: %s
                        TemperatureTime:%s
                        Weather: %s
                        """, forecast.showApiResBody.cityInfo.cityName(),
                forecast.showApiResBody.now.temperature(),
                forecast.showApiResBody.now.temperatureTime(),
                forecast.showApiResBody.now.weather());
    }

    public static void main(String[] args) {
        WeatherService client = new WeatherService();
        System.out.println(client.getWeatherForecastByLocation(40.206,116.165));
    }

}